package com.dotandmedia.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.dotmedia.utils.StringUtils;
import com.opensymphony.xwork2.ActionSupport;

/**
 * This class is the action class which will be used for logging in the admin. The username of the admin will be "admin" and the password will also be
 * the same
 * 
 * @author siddharthsaha
 * 
 */
public class LoginAdminAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {

	private static Logger logger = Logger.getLogger("LoginAdminAction");

	private static String LOGGED_ON = "logged.on";
	private static String ADMIN_LOGGED_ON = "admin.logged.on";

	// Username and password which will be read from the form
	private String username;
	private String password;

	protected Map session;
	protected HttpServletRequest request;
	protected HttpServletResponse response;

	@Override
	public void setServletResponse(HttpServletResponse resp) {
		this.response = resp;
	}

	@Override
	public void setServletRequest(HttpServletRequest req) {
		this.request = req;
	}

	@Override
	public void setSession(Map session) {
		this.session = session;
	}

	// Getters and Setters
	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsername() {
		return username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}

	// Action method
	public String loginAdmin() {
		logger.info("IN LoginAdminAction");
		String returnValue = "";
		if (StringUtils.isStringNull(username)) {
			logger.error("Username in LoginAdminAction is null. Nothing to do");
			returnValue = ERROR;
		} else if (StringUtils.isStringNull(password)) {
			logger.error("Password in LoginAdminAction is null. Nothing to do");
			returnValue = ERROR;
		} else {
			if (username.equals("admin") && password.equals("admin")) {
				logger.info("Username and Password match in LoginAdminAction");
				returnValue = SUCCESS;
				session.put(LOGGED_ON, ADMIN_LOGGED_ON);
			} else {
				logger.error("Username and Password do not match in LoginAdminAction");
				returnValue = ERROR;
			}
		}
		logger.info("OUT LoginAdminAction");
		return returnValue;
	}
}
